﻿using Knlght.Domain;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Knight.Common.Excel
{
    /// <summary>
    /// Excel操作类
    /// </summary>
    /// Microsoft Excel 11.0 Object Library
    public class ExcelHelper
    {
        #region 数据导出至Excel文件


        /// <summary>
        /// 导出Excel文件，自动返回可下载的文件流 
        /// </summary>
        /// <param name="dtData"></param>
        public static void DataTable1Excel(System.Data.DataTable dtData)
        {
            var curContext = HttpContext.Current;
            if (dtData == null) return;
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            curContext.Response.Charset = "utf-8";
            var strWriter = new StringWriter();
            var htmlWriter = new HtmlTextWriter(strWriter);
            var gvExport = new GridView();
            gvExport.DataSource = dtData.DefaultView;
            gvExport.AllowPaging = false;
            gvExport.DataBind();
            gvExport.RenderControl(htmlWriter);
            curContext.Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=gb2312\"/>" + strWriter);
            curContext.Response.End();
        }

        /// <summary>
        /// 导出Excel文件，转换为可读模式
        /// </summary>
        public static void DataTable2Excel(DataTable dtData)
        {
            var curContext = HttpContext.Current;
            if (dtData == null) return;
            curContext.Response.ContentType = "application/vnd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "";
            var strWriter = new StringWriter();
            var htmlWriter = new HtmlTextWriter(strWriter);
            var dgExport = new DataGrid
            {
                DataSource = dtData.DefaultView,
                AllowPaging = false
            };
            dgExport.DataBind();
            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }

        /// <summary>
        /// 导出Excel文件，并自定义文件名
        /// </summary>
        public static void DataTable3Excel(DataTable dtData, String FileName)
        {
            var curContext = HttpContext.Current;
            if (dtData == null) return;
            string urlEncode = HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);
            curContext.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls");
            curContext.Response.ContentType = "application nd.ms-excel";
            curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
            curContext.Response.Charset = "GB2312";
            var strWriter = new StringWriter();
            var htmlWriter = new HtmlTextWriter(strWriter);
            var dgExport = new GridView();
            dgExport.DataSource = dtData.DefaultView;
            dgExport.AllowPaging = false;
            dgExport.DataBind();
            dgExport.RenderControl(htmlWriter);
            curContext.Response.Write(strWriter.ToString());
            curContext.Response.End();
        }

        /// <summary>
        /// 将数据导出至Excel文件
        /// </summary>
        /// <param name="table">DataTable对象</param>
        /// <param name="excelFilePath">Excel文件路径</param>
        public static bool OutputToExcel(DataTable table, string excelFilePath)
        {
            if (File.Exists(excelFilePath))
            {
                throw new Exception("该文件已经存在！");
            }

            if ((table.TableName.Trim().Length == 0) || (table.TableName.ToLower() == "table"))
            {
                table.TableName = "Sheet1";
            }

            //数据表的列数
            int colCount = table.Columns.Count;

            //用于记数，实例化参数时的序号
            int i = 0;

            //创建参数
            OleDbParameter[] para = new OleDbParameter[colCount];

            //创建表结构的SQL语句
            string tableStructStr = @"Create Table " + table.TableName + "(";

            //连接字符串
            string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 8.0;";
            OleDbConnection objConn = new OleDbConnection(connString);

            //创建表结构
            OleDbCommand objCmd = new OleDbCommand();

            //数据类型集合
            ArrayList dataTypeList = new ArrayList();
            dataTypeList.Add("System.Decimal");
            dataTypeList.Add("System.Double");
            dataTypeList.Add("System.Int16");
            dataTypeList.Add("System.Int32");
            dataTypeList.Add("System.Int64");
            dataTypeList.Add("System.Single");

            //遍历数据表的所有列，用于创建表结构
            foreach (DataColumn col in table.Columns)
            {
                //如果列属于数字列，则设置该列的数据类型为double
                if (dataTypeList.IndexOf(col.DataType.ToString()) >= 0)
                {
                    para[i] = new OleDbParameter("@" + col.ColumnName, OleDbType.Double);
                    objCmd.Parameters.Add(para[i]);

                    //如果是最后一列
                    if (i + 1 == colCount)
                    {
                        tableStructStr += col.ColumnName + " double)";
                    }
                    else
                    {
                        tableStructStr += col.ColumnName + " double,";
                    }
                }
                else
                {
                    para[i] = new OleDbParameter("@" + col.ColumnName, OleDbType.VarChar);
                    objCmd.Parameters.Add(para[i]);

                    //如果是最后一列
                    if (i + 1 == colCount)
                    {
                        tableStructStr += col.ColumnName + " varchar)";
                    }
                    else
                    {
                        tableStructStr += col.ColumnName + " varchar,";
                    }
                }
                i++;
            }

            //创建Excel文件及文件结构
            try
            {
                objCmd.Connection = objConn;
                objCmd.CommandText = tableStructStr;

                if (objConn.State == ConnectionState.Closed)
                {
                    objConn.Open();
                }
                objCmd.ExecuteNonQuery();
            }
            catch (Exception exp)
            {

                throw new EJApplicationException(exp.Message);
            }

            //插入记录的SQL语句
            var insertSql1 = "Insert into " + table.TableName + " (";
            var insertSql2 = " Values (";
            var insertSql = "";

            //遍历所有列，用于插入记录，在此创建插入记录的SQL语句
            for (int colId = 0; colId < colCount; colId++)
            {
                if (colId + 1 == colCount)  //最后一列
                {
                    insertSql1 += table.Columns[colId].ColumnName + ")";
                    insertSql2 += "@" + table.Columns[colId].ColumnName + ")";
                }
                else
                {
                    insertSql1 += table.Columns[colId].ColumnName + ",";
                    insertSql2 += "@" + table.Columns[colId].ColumnName + ",";
                }
            }

            insertSql = insertSql1 + insertSql2;

            //遍历数据表的所有数据行
            for (var rowId = 0; rowId < table.Rows.Count; rowId++)
            {
                for (int colId = 0; colId < colCount; colId++)
                {
                    if (para[colId].DbType == DbType.Double && table.Rows[rowId][colId].ToString().Trim() == "")
                    {
                        para[colId].Value = 0;
                    }
                    else
                    {
                        para[colId].Value = table.Rows[rowId][colId].ToString().Trim();
                    }
                }
                try
                {
                    objCmd.CommandText = insertSql;
                    objCmd.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    string str = exp.Message;
                }
            }
            try
            {
                if (objConn.State == ConnectionState.Open)
                {
                    objConn.Close();
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return true;
        }

        /// <summary>
        /// 将数据导出至Excel文件
        /// </summary>
        /// <param name="table">DataTable对象</param>
        /// <param name="Columns">要导出的数据列集合</param>
        /// <param name="ExcelFilePath">Excel文件路径</param>
        public static bool OutputToExcel(DataTable table, ArrayList Columns, string ExcelFilePath)
        {
            if (File.Exists(ExcelFilePath))
            {
                throw new Exception("该文件已经存在！");
            }

            //如果数据列数大于表的列数，取数据表的所有列
            if (Columns.Count > table.Columns.Count)
            {
                for (int s = table.Columns.Count + 1; s <= Columns.Count; s++)
                {
                    Columns.RemoveAt(s);   //移除数据表列数后的所有列
                }
            }

            //遍历所有的数据列，如果有数据列的数据类型不是 DataColumn，则将它移除
            DataColumn column = new DataColumn();
            for (int j = 0; j < Columns.Count; j++)
            {
                try
                {
                    column = (DataColumn)Columns[j];
                }
                catch (Exception)
                {
                    Columns.RemoveAt(j);
                }
            }
            if ((table.TableName.Trim().Length == 0) || (table.TableName.ToLower() == "table"))
            {
                table.TableName = "Sheet1";
            }

            //数据表的列数
            int ColCount = Columns.Count;

            //创建参数
            OleDbParameter[] para = new OleDbParameter[ColCount];

            //创建表结构的SQL语句
            string TableStructStr = @"Create Table " + table.TableName + "(";

            //连接字符串
            string connString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelFilePath + ";Extended Properties=Excel 8.0;";
            OleDbConnection objConn = new OleDbConnection(connString);

            //创建表结构
            OleDbCommand objCmd = new OleDbCommand();

            //数据类型集合
            ArrayList DataTypeList = new ArrayList();
            DataTypeList.Add("System.Decimal");
            DataTypeList.Add("System.Double");
            DataTypeList.Add("System.Int16");
            DataTypeList.Add("System.Int32");
            DataTypeList.Add("System.Int64");
            DataTypeList.Add("System.Single");

            DataColumn col = new DataColumn();

            //遍历数据表的所有列，用于创建表结构
            for (int k = 0; k < ColCount; k++)
            {
                col = (DataColumn)Columns[k];

                //列的数据类型是数字型
                if (DataTypeList.IndexOf(col.DataType.ToString().Trim()) >= 0)
                {
                    para[k] = new OleDbParameter("@" + col.Caption.Trim(), OleDbType.Double);
                    objCmd.Parameters.Add(para[k]);

                    //如果是最后一列
                    if (k + 1 == ColCount)
                    {
                        TableStructStr += col.Caption.Trim() + " Double)";
                    }
                    else
                    {
                        TableStructStr += col.Caption.Trim() + " Double,";
                    }
                }
                else
                {
                    para[k] = new OleDbParameter("@" + col.Caption.Trim(), OleDbType.VarChar);
                    objCmd.Parameters.Add(para[k]);

                    //如果是最后一列
                    if (k + 1 == ColCount)
                    {
                        TableStructStr += col.Caption.Trim() + " VarChar)";
                    }
                    else
                    {
                        TableStructStr += col.Caption.Trim() + " VarChar,";
                    }
                }
            }

            //创建Excel文件及文件结构
            try
            {
                objCmd.Connection = objConn;
                objCmd.CommandText = TableStructStr;

                if (objConn.State == ConnectionState.Closed)
                {
                    objConn.Open();
                }
                objCmd.ExecuteNonQuery();
            }
            catch (Exception exp)
            {
                throw exp;
            }

            //插入记录的SQL语句
            string InsertSql_1 = "Insert into " + table.TableName + " (";
            string InsertSql_2 = " Values (";
            string InsertSql = "";

            //遍历所有列，用于插入记录，在此创建插入记录的SQL语句
            for (int colID = 0; colID < ColCount; colID++)
            {
                if (colID + 1 == ColCount)  //最后一列
                {
                    InsertSql_1 += Columns[colID].ToString().Trim() + ")";
                    InsertSql_2 += "@" + Columns[colID].ToString().Trim() + ")";
                }
                else
                {
                    InsertSql_1 += Columns[colID].ToString().Trim() + ",";
                    InsertSql_2 += "@" + Columns[colID].ToString().Trim() + ",";
                }
            }

            InsertSql = InsertSql_1 + InsertSql_2;

            //遍历数据表的所有数据行
            DataColumn DataCol = new DataColumn();
            for (int rowID = 0; rowID < table.Rows.Count; rowID++)
            {
                for (int colID = 0; colID < ColCount; colID++)
                {
                    //因为列不连续，所以在取得单元格时不能用行列编号，列需得用列的名称
                    DataCol = (DataColumn)Columns[colID];
                    if (para[colID].DbType == DbType.Double && table.Rows[rowID][DataCol.Caption].ToString().Trim() == "")
                    {
                        para[colID].Value = 0;
                    }
                    else
                    {
                        para[colID].Value = table.Rows[rowID][DataCol.Caption].ToString().Trim();
                    }
                }
                try
                {
                    objCmd.CommandText = InsertSql;
                    objCmd.ExecuteNonQuery();
                }
                catch (Exception exp)
                {
                    string str = exp.Message;
                }
            }
            try
            {
                if (objConn.State == ConnectionState.Open)
                {
                    objConn.Close();
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
            return true;
        }
        #endregion

        /// <summary>
        /// 获取Excel文件数据表列表
        /// </summary>
        public static ArrayList GetExcelTables(string excelFileName)
        {
            var tablesList = new ArrayList();
            if (!File.Exists(excelFileName)) return tablesList;
            using (var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + excelFileName))
            {
                DataTable dt;
                try
                {
                    conn.Open();
                    dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                }
                catch (Exception exp)
                {
                    throw new EJApplicationException(exp.Message);
                }

                //获取数据表个数
                if (dt != null)
                {
                    int tablecount = dt.Rows.Count;
                    for (int i = 0; i < tablecount; i++)
                    {
                        string tablename = dt.Rows[i][2].ToString().Trim().TrimEnd('$');
                        if (tablesList.IndexOf(tablename) < 0)
                        {
                            tablesList.Add(tablename);
                        }
                    }
                }
            }
            return tablesList;
        }

        /// <summary>
        /// 将Excel文件导出至DataTable(第一行作为表头)
        /// </summary>
        /// <param name="excelFilePath">Excel文件路径</param>
        /// <param name="tableName">数据表名，如果数据表名错误，默认为第一个数据表名</param>
        public static DataTable InputFromExcel(string excelFilePath, string tableName)
        {
            if (!File.Exists(excelFilePath))
            {
                throw new Exception("Excel文件不存在！");
            }

            //如果数据表名不存在，则数据表名为Excel文件的第一个数据表
            var tableList = GetExcelTables(excelFilePath);

            if (tableName.IndexOf(tableName, StringComparison.Ordinal) < 0)
            {
                tableName = tableList[0].ToString().Trim();
            }

            DataTable table = new DataTable();
            OleDbConnection dbcon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFilePath + ";Extended Properties=Excel 8.0");
            OleDbCommand cmd = new OleDbCommand("select * from [" + tableName + "$]", dbcon);
            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

            try
            {
                if (dbcon.State == ConnectionState.Closed)
                {
                    dbcon.Open();
                }
                adapter.Fill(table);
            }
            catch (Exception exp)
            {
                throw new EJApplicationException(exp.Message);
            }
            finally
            {
                if (dbcon.State == ConnectionState.Open)
                {
                    dbcon.Close();
                }
            }
            return table;
        }

        /// <summary>
        /// 获取Excel文件指定数据表的数据列表
        /// </summary>
        /// <param name="excelFileName">Excel文件名</param>
        /// <param name="tableName">数据表名</param>
        public static ArrayList GetExcelTableColumns(string excelFileName, string tableName)
        {
            ArrayList colsList = new ArrayList();
            if (File.Exists(excelFileName))
            {
                using (var conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + excelFileName))
                {
                    conn.Open();
                    var dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] { null, null, tableName, null });

                    //获取列个数
                    if (dt != null)
                    {
                        int colcount = dt.Rows.Count;
                        for (int i = 0; i < colcount; i++)
                        {
                            string colname = dt.Rows[i]["Column_Name"].ToString().Trim();
                            colsList.Add(colname);
                        }
                    }
                }
            }
            return colsList;
        }
    }
}
